package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import dao.AdminDao;

public class EditPasswordServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1881243631848324137L;

	public EditPasswordServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String table = request.getParameter("table");
		String name = request.getParameter("name");
		String newP = request.getParameter("newPassword");
		
		JSONObject jsonObject = new JSONObject();
		AdminDao adminDao = null;
		boolean isSuccess = false;
		
		
		try {
			adminDao = new AdminDao();
			if("admins".equals(table)) {
				isSuccess = adminDao.editPassword(name, newP);
			}
			if(isSuccess) {
				jsonObject.put("retCode", "1");
			} else {
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "修改密码失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "EditPasswordServlet Exception");
		} finally {
			out.write(jsonObject.toString());
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}
